package org.bitmap.mm;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.bitmap.mm.util.BitmapUtils;
import org.bitmap.mm.util.LoveBitmapManager;
import org.bitmap.mm.util.OnFetchCompleteListener;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnFetchCompleteListener {
	
	
	
	
	//--------------------图片加载部分------------------------------------------------
	private final int SET_BITMAP = 10;
	android.os.Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case SET_BITMAP:
				File imageFile = (File) msg.obj;
				setBitmap(imageFile);
				break;
			}

		}
	};
	String imageurl = "http://2.img.mosh.cn/0/0/0f993006f4bacb427cdcf679474bb9f8236.jpg";
	@Override
	public void onFetchComplete(String url,File imageFile) {
		Message msg = handler.obtainMessage(SET_BITMAP);
		msg.obj = imageFile;
		handler.sendMessage(msg);
	}
	/**
	 * 设置图片，测试用
	 * @param file
	 */
	private void setBitmap(File file) {
		if (ic_image != null && file != null) {
			 Bitmap bm = BitmapFactory.decodeFile(file.toString());
			 //压缩图片文件大小,31代表31kb
			 Bitmap bm2= BitmapUtils.compressImage(bm, 31);
			ic_image.setImageBitmap(bm2);
			if (obj_size != null) {
				obj_size.setText("文件大小：" + StringHelper.prettyBytes(file.length()));
				
			}
		}
	}
	//--------------------图片加载部分------------------------------------------------
	
	ImageView ic_image;
	TextView obj_size;
	TextView min_size;
	ImageAdapter adapter;
	LoveBitmapManager bm = null;
	ListView lv = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ic_image = (ImageView) findViewById(R.id.ic_image);
		obj_size = (TextView) findViewById(R.id.obj_size);
		min_size = (TextView) findViewById(R.id.min_size);
		bm = new LoveBitmapManager(getApplication());

		List<String> url = getTestList();
		if (adapter == null) {
			adapter = new ImageAdapter(this, url, bm);
		}
		lv = (ListView) findViewById(R.id.lv_lst);
		lv.setAdapter(adapter);
		
		// 单纯的下载图片
		bm.loveSimpleLoadBitmap(imageurl, this);
		
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (bm != null) {
			bm.clearCaches();
		}
	}

	private List<String> getTestList() {
		List<String> urls = new ArrayList<String>();
		urls.add("http://img1.126.net/channel5/013278/lc310220_130507.jpg");
		urls.add("http://img1.126.net/channel5/013278/lc310220_130507.jpg");
		urls.add("http://img1.cache.netease.com/house/2013/5/6/20130506092453fa728.jpg");
		urls.add("http://imgbbs.ph.126.net/auMSdOmhDZbqylo60QWCcQ==/3386988394859328210.jpg");
		urls.add("http://imgbbs.ph.126.net/PsITt6ggBLwV24CwK7moAw==/6597919689146186601.jpg");
		urls.add("http://imgbbs.ph.126.net/kUlWUClEzXc5PNTN-xyhqw==/3856770130989412248.jpg");
		urls.add("http://imgbbs.ph.126.net/8SNUUqR-FNO5sTj6FMbhUA==/3153645639166196717.jpg");
		urls.add("http://imgbbs.ph.126.net/0XPDvfiw6eccZTPQ_IVqZw==/6597754762402014688.jpg");
		urls.add("http://imgbbs.ph.126.net/-MoRUK97t53vB9l2hUzJQQ==/6597573342983378683.jpg");
		urls.add("http://imgbbs.ph.126.net/uHuq6qYGjgjcLn0X61QLBQ==/6597573342983378691.jpg");
		urls.add("http://imgbbs.ph.126.net/SHm8m2WT5YVg82AMPzi7Yw==/6597817434564757210.jpg");
		urls.add("http://imgbbs.ph.126.net/2r-H-uG-Z5CWN0Gwt5z7Xg==/6597782250192656676.jpg");
		urls.add("http://imgbbs.ph.126.net/PAnOz5ufX6BZGDmTHbUs_g==/6597311659215467014.jpg");
		urls.add("http://imgbbs.ph.126.net/3OxcbqHyZUhQQKsMqRJxDw==/6598292423587745279.jpg");
		urls.add("http://imgbbs.ph.126.net/zdauO_IGD1AlgMc0vUNCgA==/6597929584750778907.jpg");
		urls.add("http://imgbbs.ph.126.net/I8XCCmJSSSo72x098tVSig==/6597693189750805751.jpg");
		urls.add("http://imgbbs.ph.126.net/ZvbgS61ymBTjyZSncGrdLg==/6597806439448477141.jpg");
		urls.add("http://img3.cache.netease.com/photo/0007/2013-04-26/8TCKOOCU1OQR0007.jpg");
		urls.add("http://img4.cache.netease.com/photo/0007/2013-04-26/8TCKOOE31OQR0007.jpg");
		urls.add("http://img5.cache.netease.com/photo/0007/2013-04-26/8TCKOOG81OQR0007.jpg");
		urls.add("http://img4.cache.netease.com/photo/0007/2013-04-26/8TCKOOHD1OQR0007.jpg");
		urls.add("http://img4.cache.netease.com/photo/0007/2013-04-26/8TCKOOIE1OQR0007.jpg");
		urls.add("http://img6.cache.netease.com/photo/0007/2013-04-26/8TCKOOJG1OQR0007.jpg");
		urls.add("http://img2.cache.netease.com/photo/0007/2013-04-26/8TCKOOLP1OQR0007.jpg");

		urls.add("http://img2.cache.netease.com/photo/0007/2013-04-26/8TCKOOMR1OQR0007.jpg");
		urls.add("http://img3.cache.netease.com/photo/0007/2013-04-26/8TCKOOO31OQR0007.jpg");
		urls.add("http://img4.cache.netease.com/photo/0007/2013-04-26/8TCKOOQB1OQR0007.jpg");
		urls.add("http://img6.cache.netease.com/photo/0007/2013-04-26/8TCKOOS51OQR0007.jpg");
		urls.add("http://img6.cache.netease.com/photo/0007/2013-04-26/8TCKOOT51OQR0007.jpg");
		urls.add("http://img6.cache.netease.com/photo/0007/2013-04-26/8TCKOOUB1OQR0007.jpg");
		urls.add("http://img3.cache.netease.com/photo/0007/2013-04-26/8TCKOOVH1OQR0007.jpg");

		urls.add("http://img6.cache.netease.com/photo/0007/2013-04-28/8THO014A1OQR0007.jpg");
		urls.add("http://img2.cache.netease.com/photo/0007/2013-04-28/8THO00RC1OQR0007.jpg");
		urls.add("http://img3.cache.netease.com/photo/0007/2013-04-28/8THO00PV1OQR0007.jpg");
		urls.add("http://img4.cache.netease.com/photo/0007/2013-04-28/8THO012S1OQR0007.jpg");
		urls.add("http://img5.cache.netease.com/photo/0007/2013-04-28/8THO00SL1OQR0007.jpg");
		urls.add("http://img3.cache.netease.com/photo/0007/2013-04-28/8THO00L91OQR0007.jpg");
		urls.add("http://img2.cache.netease.com/photo/0007/2013-04-28/8THO00VK1OQR0007.jpg");
		urls.add("http://img5.cache.netease.com/photo/0007/2013-04-28/8THO00IP1OQR0007.jpg");
		urls.add("http://img2.cache.netease.com/photo/0007/2013-04-28/8THO00UJ1OQR0007.jpg");

		return urls;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	

}
